﻿import { reactive, version } from 'vue';

const store = reactive({

    user : {
        userid: "",
        login_name: "",
        name: "",
        avatar: "",
        utype: 0,
        roles: []
    },
    $mbh5: /mobile|android|iphone|ipad|phone/i.test(navigator.userAgent),//是否手机版本页面
    token : "",
    hasLogin : false,
    leftCollapse : false,//左边菜单是否打开
    showLeft : false, //是否显示左边菜单
    debug : true,
    search2 : false,//使用第二种方式传入查询参数, 查询参数放入 search.pms中
    api_action : "",//服务请求时候加的前缀
    dm_url:"",//dm文件路径

    hasRole(role) {
        return store.user.roles.indexOf(role) != -1;
    },

    isCreateUser(userid) {
        return store.user.userid == userid;
    },

    isAdmin() {
        return store.hasRole("管理员");
    },

    isCreateOrAdmin(userid) {
        return store.isCreateUser(userid) || store.isAdmin();
    },


    getUrl(val) {
        if (!val) return "";
        if (val.startsWith("http")) {
            return val;
        }
        if(store.UploadRoot && val.startsWith(store.UploadRoot)){
            return val;
        }
        let val1 = val.replace(/\/\//g, "/");
        if (val1 && store.UploadRoot) {
            if (val1.startsWith("../")) {
                val1 = val1.substr(3);
            }
            if (val1.startsWith("/")) {
                val1 = val1.substr(1);
            }
            return (store.UploadRoot + val1);
        }
        const pp = location.pathname.split("/");
        return pp.length > 4 ? "../" + val1 : val1;
    },

    config(cfg) {
        Object.keys(cfg).forEach(key=>{
            store[key] = cfg[key];
        })
    },


    /***
     * MyDialog纳入history管理， 用户点后退按钮的时候，可以关闭当前打开的对话框
     */
     __dlgs: [],


     pushDlg(dlg) {
         this.__dlgs.push(dlg);
        //  history.pushState("对话框", "对话框:" + dlg.title);
     },
 
     popupDlg(dlg) {
         const ix = this.__dlgs.indexOf(dlg);
         if (ix >= 0) {
             this.__dlgs.splice(ix, 1);
             //history.back();
         }
     },
 
     closeDlg() {
         if (this.__dlgs.length > 0) {
             let dlg = this.__dlgs.pop();
             dlg.close();
         }
     },

     //取当前最顶上显示的对话框
     getTopDlg(){
        if (this.__dlgs.length > 0) {
            let dlg = this.__dlgs[this.__dlgs.length-1];
            return dlg;
        }
        return dlg;
     },

     getTopForm(){
         let dlg = this.getTopDlg();
        return dlg ? dlg["$parent"] : null;
     },

 
});


window.onpopstate = function (e) {
    //微信小程序中，hash变化也会触发onpopstate，此时不关闭dlg，例如上传文件的对话框，还有上次有人报告选择文件以后就自动关闭了form
    if(event.state){
        store.closeDlg()
    }
}

export default store;

